package com.example.sls.mapper.Class;

import com.example.sls.pojo.Class._Class;
import com.example.sls.pojo.student.Student;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.jta.UserTransactionAdapter;

import java.util.List;
import java.util.Map;

@Mapper
@Repository

public interface ClassMapper {

    @Select("select * from class where tId = #{teacherId}")
    @Results(id = "classMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "classId", property = "classId"),
            @Result(column = "tId", property = "tId"),
            @Result(column = "cnt", property = "cnt"),
            @Result(column = "classId", property = "students", many =
            @Many(select = "com.example.sls.mapper.student.StudentMapper.getStudentsByCId", fetchType = FetchType.DEFAULT))
    })
    List<_Class> getClassesByTId(@Param("teacherId") Integer teacherId);  //通过教师号获取该教师管理的所有班级


    @Select("select * from class")
    @ResultMap("classMap")
    List<_Class> getClassesInfo();  //查询所有班级信息

    @Select("select * from class where classId = #{CId} ; ")
    @ResultMap("classMap")
    _Class getClassInfoByCId(@Param("CId")Integer CId);  //通过CId获取对应班级信息

    @Select("select cnt from class where classId = #{CId} ; ")
    Integer getCountByCId(@Param("CId")Integer CId);   //获取班级id为cId的学生人数


    @Update("update class set cnt = #{cnt} where classId = #{CId};")
    Integer setCountByCId(@Param("CId")Integer CId, @Param("cnt")Integer cnt);  //更新学生人数

    @Insert("insert into class (id, classId, tId, cnt) values (#{id}, #{classId}, #{tId}, #{cnt});")
    Integer addClass(Map<String, Object> map);

    @Select("select max(id) from class;")
    Integer getMaxId();

    @Select("select count(*) from class;")
    Integer getClassCount();   //获取表中数据条数



}
